package com.zxjbyte.yiyi.module.system.controller.dict;

import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.domain.Kv;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.framework.common.util.CollXUtil;
import com.zxjbyte.yiyi.module.system.domain.entity.DictTypePO;
import com.zxjbyte.yiyi.module.system.domain.request.dict.DictTypeAddRequest;
import com.zxjbyte.yiyi.module.system.domain.request.dict.DictTypePageRequest;
import com.zxjbyte.yiyi.module.system.domain.request.dict.DictTypeUpdateRequest;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictTypeDetailResponse;
import com.zxjbyte.yiyi.module.system.domain.response.dict.DictTypePageResponse;
import com.zxjbyte.yiyi.module.system.manager.DictManager;
import com.zxjbyte.yiyi.module.system.service.dict.IDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zhangxingjia
 * @Date 2024/1/24 16:12
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "字典类型")
@RestController
@RequestMapping(value = Constant.BASE_PATH_B + "/system/dicttype")
public class DictTypeController {

    @Resource
    private IDictTypeService dictTypeService;
    @Resource
    private DictManager dictManager;

    @Operation(summary = "字典类型分页查询")
    @GetMapping("/page")
    public PageResult<DictTypePageResponse> page(@ParameterObject DictTypePageRequest request){
        return dictTypeService.page(request);
    }

    @Operation(summary = "字典类型详情查询")
    @GetMapping("/detail")
    public R<DictTypeDetailResponse> detail(@Parameter(description = "唯一id" ) @RequestParam Long id){
        return R.data(dictTypeService.detail(id));
    }

    @Operation(summary = "新增字典类型")
    @PostMapping("/add")
    public R<?> add(@Valid @RequestBody DictTypeAddRequest request){
        dictTypeService.add(request);
        return R.ok();
    }

    @Operation(summary = "编辑字典类型")
    @PutMapping("/update")
    public R<?> update(@Valid @RequestBody DictTypeUpdateRequest request){
        dictManager.dictTypeUpdate(request);
        return R.ok();
    }

    @Operation(summary = "删除字典类型")
    @DeleteMapping("/del")
    public R<?> delete(@Parameter(description = "唯一id" ) @RequestParam Long id){
        dictManager.dictTypeDelete(id);
        return R.ok();
    }

    @Operation(summary = "字典类型下拉框")
    @GetMapping("/dropdown")
    public R<List<Kv>> dropdown(@Parameter(description = "字典类型名称") @RequestParam(required = false) String name){
        List<DictTypePO> list = dictTypeService.getByName(name);
        if(CollXUtil.isNotEmpty(list)){
            List<Kv> resultList = list.stream().map(s -> Kv.create().set("value", s.getType()).set("label", s.getName()))
                    .collect(Collectors.toList());
            return R.data(resultList);
        }
        return R.ok();
    }
}
